 //地理编码插件，用于通过坐标获取地址信息
 var geocoder = new AMap.Geocoder();

 //添加定位组件，用于获取用户当前的精确位置Geocoder
 var geolocation = new AMap.Geolocation({
 	showCircle: false, //不显示定位结果的圆
 	showMarker: false, //不现实定位结果的标记
 	showButton: false, //不现实组件的定位按钮
 	timeout: 5000 //浏览器定位超时时间5s
 });
 map.addControl(geolocation);

 //body onload时调用

 //信息显示
 var infoDiv = document.getElementsByClassName('info')[0];
 window.showTip = function(text) {
 	infoDiv.innerHTML = text;
 	infoDiv.className = 'info top showOnce';
 }
 infoDiv.addEventListener('animationend',function(){
	 this.classList.remove('showOnce');
 });

 //起始位置
 var origin = document.getElementById('origin');
 //目的地
 var destination = document.getElementById('destination');
 var showOriginAddress = function(address) {
 	origin.innerHTML = address
 };
 var showDestinationAddress = function(address) {
 	destination.innerHTML = address;
 };

 //定位失败之后进行城市定位
 var onLocateFailed = function() {
		geolocation.getCityInfo(function(status, result) {
 		map.setZoom(14);
 		showLocation(result.center); //在城市中心点显示起始marker
			starCurPos = result.center;
 		placeSearch.setCity(result.citycode);
 		autoComplete.setCity(result.citycode);
 	})
 };
 //定位成功
 var onLocateSuccess = function(result) {
 	// showTip('定位成功');
 	showLocation(result.position); //在定位结果显示起始marker
	 starCurPos = result.position;
	 var city = result.addressComponent.city;
 	var province = result.addressComponent.province;
 	var district = result.addressComponent.district;
 	var township = result.addressComponent.township;
 	showOriginAddress(result.formattedAddress.replace(province, '').replace(city, '').replace(district, '').replace(township, ''))
 	origin.position = result.position;
 	placeSearch.setCity(result.addressComponent.citycode);
 	autoComplete.setCity(result.addressComponent.citycode);
 };
 function startLocate(callback) {
	 document.getElementById('locating').style.display = 'block';
	 geolocation.getCurrentPosition(function(status, result) {
		 console.log(result);
		 if (status == 'complete') {
			 onLocateSuccess(result) //定位成功
			 callback();
		 } else if (status == 'error') {
			 //定位失败
			 if (result.message.indexOf('Geolocation permission denied.') !== -1) {
				 //Geolocation permission denied.表示用户禁用了浏览器或者APP的定位权限或者关闭了手机的定为服务
				 //或者当前页面为非安全页面,Chrome或者IOS10等系统会禁用非安全页面的定位请求，如果您的页面还没有支持HTTPS请尽快升级
				 //安全页面指的是支持HTTPS的Web站点，而且是通过https协议打开的页面。安全页面也包括本地页面
				 showTip('您好，请在系统的隐私设置中打开当前应用的定位权限。');
			 } else {
				 showTip('无法获取精确位置,将定位您所在的城市。');
			 }
			 onLocateFailed();
			 callback();
		 }
	 })
 };
 window.startLocate = startLocate;
 //显示起始marker，并开启拖拽调整起始位置的功能
 var showLocation = function(position) {
 	console.log(position)
 	document.getElementById('locating').style.display = 'none';
 	startMarker.setPosition(position);
 	startMarker.show();
 	startMarker.setMap(map);
 	// map.setCenter(position);
 	// startAdjustOrigin(); //开启拖拽地图调整定位点
 	enableSearch(); //定位有结果之后才允许搜索
 }

 //拖拽开始的时候添加控件，隐藏起始marker，注册拖拽结束事件
 var onMapDragStart = function() {
 	// startMarker.hide();
 };
 var startAdjustOrigin = function() {
 	map.on('dragstart', onMapDragStart);
 }